//
//  PKGroupMainViewController.m
//  PianKe
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//
#define topicUrl @"http://api2.pianke.me/group/posts_hotlist"
#define groupUrl @"http://api2.pianke.me/group/group_list"


#import "PKGroupMainViewController.h"

#import "PKGroupTopicViewController.h"
#import "PKGroupTopicLatestViewController.h"
#import "PKGroupGroupViewController.h"
#import "PKGroupGroupLatestViewController.h"

@interface PKGroupMainViewController ()<UIScrollViewDelegate,UITableViewDelegate>
@property(nonatomic,strong)UIButton *topicBt;
@property(nonatomic,strong)UIButton *groupBt;
@property(nonatomic,strong)UIButton *selfBt;

@property(nonatomic,strong)UISegmentedControl *seg;
@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)PKGroupTopicViewController *topicHottestVC;
@property(nonatomic,strong)PKGroupTopicLatestViewController *topicLatestVC;
@property(nonatomic,strong)PKGroupGroupViewController *groupHottestVC;
@property(nonatomic,strong)PKGroupGroupLatestViewController *groupLatestVC;


@property(nonatomic,strong)UITableView *hottestTableView;
@property(nonatomic,strong)UITableView *latestTableView;
@property(nonatomic,copy)NSString *sort;
@property(nonatomic,copy)NSString *themeUrl;
@end

@implementation PKGroupMainViewController



-(void)reSetTopView
{
    _topicBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _topicBt.frame = CGRectMake(100, 25, 40, 20);
    [_topicBt setTitle:@"话题" forState:UIControlStateNormal];
    [_topicBt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_topicBt addTarget:self action:@selector(turnToTopic) forControlEvents:UIControlEventTouchUpInside];
    
    _groupBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _groupBt.frame = CGRectMake(150, 25, 40, 20);
    [_groupBt setTitle:@"小组" forState:UIControlStateNormal];
    [_groupBt setTintColor:[UIColor whiteColor]];
    [_groupBt addTarget:self action:@selector(turnToGroup) forControlEvents:UIControlEventTouchUpInside];
    
    _selfBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _selfBt.frame = CGRectMake(210, 25, 40, 20);
    [_selfBt setTitle:@"我的" forState:UIControlStateNormal];
    [_selfBt setTintColor:[UIColor whiteColor]];
//    [_selfBt addTarget:self action:@selector(turnToSelf) forControlEvents:UIControlEventTouchUpInside];
    
    
    [_topView addSubview:_topicBt];
    [_topView addSubview:_groupBt];
    [_topView addSubview:_selfBt];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    self.titleLabel.text = @"社区";
    [self reSetTopView];
    
    _seg = [[UISegmentedControl alloc]initWithItems:@[@"最热",@"最新"]];
    _seg.frame = CGRectMake(0, 70, SCREEN_WITDTH, 30);
    _seg.layer.cornerRadius = 0;
    _seg.selectedSegmentIndex = 0;
//    [_seg addTarget:self action:@selector(changeIndex) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_seg];
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WITDTH, SCREEN_HEIGHT-100)];
    _scrollView.contentSize = CGSizeMake(SCREEN_WITDTH*4, SCREEN_HEIGHT-100);
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    

    [self addVC];
    
    
}

-(void)addVC
{
    _topicHottestVC = [[PKGroupTopicViewController alloc]init];
    _topicHottestVC.groupMainVC = self;
    _topicHottestVC.view.frame = CGRectMake(0, 0, SCREEN_WITDTH, SCREEN_HEIGHT-100) ;
    [_scrollView addSubview: _topicHottestVC.view];
    
    _topicLatestVC = [[PKGroupTopicLatestViewController alloc]init];
    _topicLatestVC.groupMainVC = self;
    _topicLatestVC.view.frame = CGRectMake(SCREEN_WITDTH, 0, SCREEN_WITDTH, SCREEN_HEIGHT-100);
    [_scrollView addSubview:_topicLatestVC.view];
    
    _groupHottestVC = [[PKGroupGroupViewController alloc]init];
    _groupHottestVC.groupMainVC = self;
    _groupHottestVC.view.frame = CGRectMake(SCREEN_WITDTH * 2, 0, SCREEN_WITDTH, SCREEN_HEIGHT-100);
    [_scrollView addSubview:_groupHottestVC.view];
    
    _groupLatestVC = [[PKGroupGroupLatestViewController alloc]init];
    _groupLatestVC.groupMainVC = self;
    _groupLatestVC.view.frame = CGRectMake(SCREEN_WITDTH * 3, 0, SCREEN_WITDTH, SCREEN_HEIGHT-100);
    [_scrollView addSubview:_groupLatestVC.view];
    
    
}


-(void)turnToTopic
{
    _scrollView.contentOffset = CGPointMake(0, 0);
    _seg.selectedSegmentIndex = 0;
}

-(void)turnToGroup
{
    _scrollView.contentOffset =  CGPointMake(SCREEN_WITDTH * 2, 0);
    _seg.selectedSegmentIndex = 0;
}



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat X = _scrollView.contentOffset.x;
//    for (UIView *subViews in _scrollView.subviews) {
//        [subViews removeFromSuperview];
//    }
    
    
    if (X==SCREEN_WITDTH) {

        _seg.selectedSegmentIndex = 1;
        [_groupBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_topicBt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        
    }else if(X == SCREEN_WITDTH * 2){
        _seg.selectedSegmentIndex = 0;
        [_groupBt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_topicBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else if (X == SCREEN_WITDTH * 3){
        _seg.selectedSegmentIndex = 1;
        [_groupBt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_topicBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else if(X == 0){
        _seg.selectedSegmentIndex = 0;
        [_groupBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_topicBt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        
    }

        
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
